package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.VoiceVo;
import com.tanhua.dubbo.server.service.IdService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class VoiceApiImpl implements VoiceApi {

    @Autowired
    private IdService idService;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 上传语音
     * @param voiceVo
     * @return
     */
    @Override
    public String save(VoiceVo voiceVo) {
        if(voiceVo.getUserId() == null){
            return null;
        }

        voiceVo.setId(ObjectId.get());
       // voiceVo.setCreated(System.currentTimeMillis());
        //自增长ID
        voiceVo.setSid(this.idService.createId("voices", voiceVo.getId().toHexString()));//objectId转String类型
        //上传
        this.mongoTemplate.save(voiceVo);
        return voiceVo.getId().toHexString();

    }

    @Override
    public List<VoiceVo> getVoice() {
        List<VoiceVo> voiceVos = mongoTemplate.find(new Query(), VoiceVo.class);
        return voiceVos;
    }
}
